<?php
class AdminsController extends AppController {
	public $uses = array (
		'Product',
		'Category',
		'Article',
		'User',
		'Message',
		'Order',
		'Configuration'
	);

	public function beforeFilter() {
		parent :: beforeFilter();
		$this->Auth->deny('*');
		$this->Auth->allow('login');
		$this->Auth->loginAction['controller'] = 'admins';
		$this->Auth->loginRedirect['controller'] = 'admins';
		$this->Auth->logoutRedirect['controller'] = 'admins';
		$this->Auth->authenticate['Form'] = array('userModel' => 'Admin', 'fields' => array('username' => 'user_name'));
		Authcomponent :: setSessionKey("Auth.Admin");
		$this->layout = "admins_none";
	}

	protected function _getAdminLeftNav() {
		$nav_arr = array (
			"系统管理" => "/admins/sysmng",
			"分类管理" => array (
				"商品分类" => "/admins/categoryProduct",
				"新闻分类" => "/admins/categoryArticle",
				"新增分类" => "/admins/categoryAdd"
			),
			"商品管理" => array (
				"商品列表" => "/admins/productList",
				"新增商品" => "/admins/productAdd",
				"产品搜索" => "/admins/productSearch"
			),
			"新闻管理" => array (
				"新闻列表" => "/admins/articleList",
				"新增新闻" => "/admins/articleAdd"
			),
			"会员管理" => array (
				"会员列表" => "/admins/userList",
				"会员添加" => "/admins/userAdd"
			),
			"留言管理" => array (
				"留言列表" => "/admins/messageList/"
			),
			"订单管理" => array (
				"订单列表" => "/admins/orderList/"
			)
		);
		$return_str = "<ul>";
		foreach ($nav_arr as $k1 => $v1) {
			if (is_array($v1)) {
				$return_str .= "<li><a href='javascript:;' onclick='showOrHideNav(this);return false;'>{$k1}</a><ul>";
				foreach ($v1 as $k2 => $v2) {
					$return_str .= "<li><a href='javascript:;' onclick=\"setRightBoxSrc('{$v2}');return false;\">{$k2}</a></li>";
				}
				$return_str .= "</ul></li>";
			} else {
				$return_str .= "<li><a href='javascript:;' onclick=\"setRightBoxSrc('{$v1}');return false;\">{$k1}</a></li>";
			}
		}
		$return_str .= "</ul>";
		return $return_str;
	}

	public function add() {
		if ($this->request->is('post')) {
			$this->Admin->create();
			if ($this->Admin->save($this->request->data)) {
				$this->Session->setFlash(__('The admin has been saved'));
				$this->redirect(array (
					'action' => 'index'
				));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
	}

	public function edit($id = null) {
		$this->Admin->id = $id;
		if (!$this->Admin->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Admin->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array (
					'action' => 'index'
				));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Admin->read(null, $id);
			unset ($this->request->data['User']['password']);
		}
	}

	public function delete($id = null) {

		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}

		$this->Admin->id = $id;
		if (!$this->Admin->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}

		if ($this->Admin->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array (
			'action' => 'index'
		));
	}

	public function login() {
		if ($this->request->isPost()) {
			echo AuthComponent::$sessionKey;
			if ($this->Auth->login($this->Admin)) {
				$this->redirect($this->Auth->redirect());
			} else {
				$this->Session->setFlash(__('Invalid username or password, try again'));
			}
		}
		$this->layout = "default";
	}

	public function logout() {
		$this->Session->write("LoginRole", "N");
		$this->redirect($this->Auth->logout());
	}

	public function index() {
		$this->layout = "admins";
		$this->set("admin_left_nav", $this->_getAdminLeftNav());
	}
	public function home() {

	}
	public function sysmng() {

	}

	/**
	 * cat mng
	 */
	public function categoryAdd() {

	}
	public function categoryArticle() {
		$cates = $this->Category->find("all",array("conditions"=>array('type'=>'news')));
		print_r($cates);
	}
	public function categoryProduct() {
		$cates = $this->Category->find("all",array("conditions"=>array('type'=>'product')));
		print_r($cates);
	}

	/**
	 * product  mng
	 */
	public function productList() {

	}
	public function productAdd() {

	}

	public function productSearch() {

	}

	/***
	 * news mng
	 */
	public function articleList() {

	}
	public function articleAdd() {

	}

	/**
	 * member mng
	 */
	public function userList() {

	}

	public function userAdd() {

	}

	/**
	 * msg mng
	 */
	public function messageList() {

	}

	/**
	 * order mng
	 */
	public function orderList() {

	}

}
?>